﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Windows.Threading;

namespace CitiSurface
{
    /// <summary>
    /// Interaction logic for AlbumControl.xaml
    /// </summary>
    public partial class AlbumControl : UserControl
    {

        public static readonly RoutedEvent CloseClickEvent;

        public event RoutedEventHandler CloseClick
        {
            add { AddHandler(CloseClickEvent, value); }
            remove { RemoveHandler(CloseClickEvent, value); }
        }

        static AlbumControl()
        {

            CloseClickEvent = EventManager.RegisterRoutedEvent("CloseClick", RoutingStrategy.Bubble,
                                                               typeof (RoutedEventHandler), typeof (AlbumControl));
        }

        private DispatcherTimer _controlPanelTimer;

        public AlbumControl()
        {
            InitializeComponent();
        }

        private void CancelButtonClick(object sender, RoutedEventArgs e)
        {
            // cancel button has been click
            // raised the closeclick event of the user control
            RaiseEvent(new RoutedEventArgs(CloseClickEvent, this));
        }

        private void BackButtonClick(object sender, RoutedEventArgs e)
        {
            //switch back to previous index
            elementFlow.SelectedIndex = elementFlow.SelectedIndex - 1;
            _controlPanelTimer.Start();
        }

        private void NextButtonClick(object sender, RoutedEventArgs e)
        {
            //next index
            elementFlow.SelectedIndex = elementFlow.SelectedIndex +1;
            _controlPanelTimer.Start();
        }

        private void ElementFlowTouchDown(object sender, TouchEventArgs e)
        {
            //Show the control panel
            CancelButton.Visibility = Visibility.Visible;
            BackButton.Visibility = Visibility.Visible;
            NextButton.Visibility = Visibility.Visible;

            //set auto fade out timeout
            var controlPanelFadein = (Storyboard)FindResource("ControlPanelFadein");
            controlPanelFadein.Begin();
            if (_controlPanelTimer == null)
            {
                _controlPanelTimer = new DispatcherTimer();
                _controlPanelTimer.Interval = TimeSpan.FromSeconds(4);
                _controlPanelTimer.Tick += ControlPanelTimerTick;
            }
            _controlPanelTimer.Start();
        }

        void ControlPanelTimerTick(object sender, EventArgs e)
        {
            //auto fadeout timeout
            //hide the control panel and stop the timer
            var controlPanelFadeout = (Storyboard)FindResource("ControlPanelFadeout");
            controlPanelFadeout.Begin();
            controlPanelFadeout.Completed += (s, t) =>
                                                 {
                                                     CancelButton.Visibility = Visibility.Collapsed;
                                                     BackButton.Visibility = Visibility.Collapsed;
                                                     NextButton.Visibility = Visibility.Collapsed;
                                                 };
            _controlPanelTimer.Stop();
        }
    }
}
